//
//  SISSPhotoViewer.m
//  SISSAPP
//
//  Created by 优房网 on 2017/3/27.
//  Copyright © 2017年 优房网. All rights reserved.
//

#import "SISSPhotoViewer.h"
#import <SDWebImage/SDWebImageManager.h>

@interface SISSPhotoViewer ()

@end

@implementation SISSPhotoViewer

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupUI];
//    self.view.backgroundColor = [UIColor yyr_randomColor];
}



- (void)setupUI {
    self.view.backgroundColor = [UIColor whiteColor];
    // scrollView
    UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:self.view.bounds];
    [self.view addSubview:scrollView];
    
    // imageView
    UIImageView *imageView = [[UIImageView alloc] init];
//    imageView.clipsToBounds = YES;
    imageView.contentMode = UIViewContentModeScaleAspectFit;
    [scrollView addSubview:imageView];
    
    if (self.image) {
        imageView.image = self.image;
        CGSize size = self.image.size;
        CGFloat height = size.height * SCREEN_WIDTH / size.width;
        CGRect rect = CGRectMake(0, 0, size.width, height);
        imageView.frame = rect;
        
        if (height <= SCREEN_HEIGHT) {
            imageView.center = scrollView.center;
        } else {
            scrollView.contentSize = CGSizeMake(size.width, height);
        }
    }
    if (!self.url) return;
    [[SDWebImageManager sharedManager] downloadImageWithURL:[NSURL URLWithString:self.url] options:0 progress:nil completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished, NSURL *imageURL) {
        
        if (!error) {
            imageView.image = image;
            CGSize size = image.size;
            CGFloat height = size.height * SCREEN_WIDTH / size.width;
            CGRect rect = CGRectMake(0, 0, size.width, height);
            imageView.frame = rect;
            
            if (height <= SCREEN_HEIGHT) {
                imageView.center = scrollView.center;
            } else {
                scrollView.contentSize = CGSizeMake(size.width, height);
            }

        }
    }];
    
}

@end
